package com.newtouch.bxzs.database.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.newtouch.bxzs.common.base.BaseErrorEnum;
import com.newtouch.bxzs.common.base.BaseException;
import com.newtouch.bxzs.common.base.model.User;
import com.newtouch.bxzs.common.base.model.bo.UserDetailBO;
import com.newtouch.bxzs.common.base.util.EntityToBeanUtils;
import com.newtouch.bxzs.common.base.util.UserDetailUtil;
import com.newtouch.bxzs.database.mapper.UserMapper;
import com.newtouch.bxzs.database.model.bo.MemberInfoBO;
import com.newtouch.bxzs.database.model.vo.MemberInfoVO;
import com.newtouch.bxzs.database.service.MemberInfoService;
import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.util.ObjectUtils;
import org.springframework.util.StringUtils;

/**
 * 机构成员信息业务层
 * <b>History:</b>
 * <table border="1">
 * <tr>
 * <th>Date</th>
 * <th>Operator</th>
 * <th>Memo</th>
 * </tr>
 * <tr>
 * <td>2021/2/23 9:41</td>
 * <td>wangzhen</td>
 * <td>Create</td>
 * </tr>
 * </table>
 *
 * @author wangzhen
 * @version 1.0.0
 * @since 1.0.0
 */
@Service
public class MemberInfoServiceImpl implements MemberInfoService {

    /**
     * 静态变量：系统日志
     */
    private static final Log logger = LogFactory.getLog(MemberInfoServiceImpl.class);


    @Autowired
    private UserMapper userMapper;

    /**
     * (non-Javadoc)
     *
     * @see com.newtouch.bxzs.database.service.MemberInfoService#getMemberList(com.newtouch.bxzs.database.model.vo.MemberInfoVO)
     */
    @Override
    public IPage<MemberInfoBO> getMemberList(MemberInfoVO memberInfoVO) {
        //获取登录用户信息
        this.checkUserInfo();
        // 封装查询信息
        Page<User> userPage = getUserPage(memberInfoVO);
        if (userPage == null) {
            return null;
        }
        //转换响应体对象
        IPage<MemberInfoBO> page = new Page<>();
        /* 将转换的BO放到Records中 */
        setPageInfo(userPage, page);
        return page;
    }

    @Override
    public MemberInfoBO getMemberInfo(String userId) {
        //获取登录用户信息
        this.checkUserInfo();

        User user = userMapper.selectById(userId);
        MemberInfoBO memberInfoBO = new MemberInfoBO();
        BeanUtils.copyProperties(user, memberInfoBO);
        return memberInfoBO;
    }

    /**
     * 封装分页信息
     *
     * @param userPage
     * @param page
     */
    private void setPageInfo(Page<User> userPage, IPage<MemberInfoBO> page) {
        page.setRecords(EntityToBeanUtils.copyBean(userPage.getRecords(), MemberInfoBO.class));
        page.setCurrent(userPage.getCurrent());
        page.setPages(userPage.getPages());
        page.setSize(userPage.getSize());
        page.setTotal(userPage.getTotal());
    }

    private Page<User> getUserPage(MemberInfoVO memberInfoVO) {
        // 登录人判空
        String userOrganizationCode = null;
        if (UserDetailUtil.getUserDetail() != null) {
            userOrganizationCode = UserDetailUtil.getUserDetail().getUserOrganizationCode();
        }

        // 封装条件
        QueryWrapper<User> wrapper = new QueryWrapper<>();
        // 附带条件筛选时 对查询条件进行封装
        if (!StringUtils.isEmpty(memberInfoVO.getUserName())) {
            wrapper.like("USER_NAME", memberInfoVO.getUserName());
        }
        if (!StringUtils.isEmpty(memberInfoVO.getOrganization())) {
            wrapper.eq("USER_ORGANIZATION_CODE", memberInfoVO.getOrganization());
        } else {
            wrapper.eq("USER_ORGANIZATION_CODE", userOrganizationCode);
        }
        if (!StringUtils.isEmpty(memberInfoVO.getUserWorkingStatus())) {
            wrapper.eq("USER_WORKING_STATUS", memberInfoVO.getUserWorkingStatus());
        }

        wrapper.orderByDesc("USER_ENTRY_TIME");

        // 按条件查询
        Page<User> p = new Page<>(memberInfoVO.getCurrentPage(), memberInfoVO.getPageSize());
        return userMapper.selectPage(p, wrapper);
    }

    /**
     * 基本信息检查
     */
    private void checkUserInfo() {
        UserDetailBO userDetail = UserDetailUtil.getUserDetail();
        if (ObjectUtils.isEmpty(userDetail)) {
            throw new BaseException(BaseErrorEnum.USER_NOT_EXISTS);
        }
    }
}
